package com.centling.bluecollar.login.filter;

import java.util.ArrayList;  
import java.util.Collection;  
import java.util.List;  
  


import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.security.core.GrantedAuthority;  
import org.springframework.security.core.authority.SimpleGrantedAuthority;  
import org.springframework.security.core.userdetails.UserDetails;  
import org.springframework.security.core.userdetails.UserDetailsService;  
import org.springframework.security.core.userdetails.UsernameNotFoundException;  

import com.centling.bluecollar.login.DAO.IUserDAO;
import com.centling.bluecollar.login.entity.User;
  
  
public class MyUserDetailsService implements UserDetailsService {  
  
    @Autowired  
    private IUserDAO userDao;  
  
    @Override  
    public UserDetails loadUserByUsername(String username)  
            throws UsernameNotFoundException {  
        Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();  
        User user = new User();  
        try {  
            user = userDao.getUserByName(username);  
            List<String> authStr = userDao.loadUserAuthoritiesByName(username);  
            for (String authName : authStr) {  
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(  
                        authName);  
                auths.add(authority);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return new org.springframework.security.core.userdetails.User(  
                user.getUsername(), user.getPassword(), true, true, true, true,  
                auths);  
    }  
  
}  
